﻿using AntdUI;
using EONFULLSIN.Languages;
using EONFULLSIN.Models.Entity.Log;
using EONFULLSIN.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace EONFULLSIN.Views.UserView
{
    public partial class LogRecord : UserControl
    {
        public LogRecord()
        {
            InitializeComponent();
            this.btn_search.Click += Btn_search_Click;
            this.btn_result.Click += Btn_result_Click;
            InitTableColumns();
            Init();
            LanguageChangedEvent.LanguageChanged += LanguageChangedEvent_LanguageChanged;
            LanguageChangedEvent_LanguageChanged(LanguageHelper.CurrentLanguage);
        }

        private void LanguageChangedEvent_LanguageChanged(string obj)
        {
            pageHeader2.Text = LanguageHelper.GetTranslation(obj, "LogRecord_Title");
            groupBox1.Text = LanguageHelper.GetTranslation(obj, "LogRecord_GroupBox");
            label1.Text = LanguageHelper.GetTranslation(obj, "LogRecord_Lable1");
            label2.Text = LanguageHelper.GetTranslation(obj, "LogRecord_Lable2");
            label3.Text = LanguageHelper.GetTranslation(obj, "LogRecord_Lable3");
            txt_logType.PlaceholderText = LanguageHelper.GetTranslation(obj, "LogRecord_Text_LogType");
            txt_level.PlaceholderText = LanguageHelper.GetTranslation(obj, "LogRecord_Text_Level");
            dp_start.PlaceholderText = LanguageHelper.GetTranslation(obj, "LogRecord_DP_Start");
            dp_end.PlaceholderText = LanguageHelper.GetTranslation(obj, "LogRecord_DP_End");
            btn_search.Text = LanguageHelper.GetTranslation(obj, "LogRecord_Button_Search");
            btn_result.Text = LanguageHelper.GetTranslation(obj, "LogRecord_Button_Result");
            var LogColumn = tab_log.Columns;
            foreach (var item in LogColumn)
            {
                if (item.Key == "DeviceType")
                {
                    item.Title = LanguageHelper.GetTranslation(obj, "LogRecord_table_DeviceType");
                }
                if (item.Key == "Level")
                {
                    item.Title = LanguageHelper.GetTranslation(obj, "LogRecord_table_Level");
                }
                if (item.Key == "Logger")
                {
                    item.Title = LanguageHelper.GetTranslation(obj, "LogRecord_table_Logger");
                }
                if (item.Key == "Message")
                {
                    item.Title = LanguageHelper.GetTranslation(obj, "LogRecord_table_Message");
                }
                if (item.Key == "Date")
                {
                    item.Title = LanguageHelper.GetTranslation(obj, "LogRecord_table_Date");
                }
            }
            this.tab_log.Columns = LogColumn;
            tab_log.Refresh();
        }

        private void Btn_result_Click(object sender, EventArgs e)
        {
            Init();
        }

        private void Btn_search_Click(object sender, EventArgs e)
        {
            string logType = txt_logType.Text;
            string logLevel = txt_level.Text;
            DateTime? start = dp_start.Value;
            DateTime? end = dp_end.Value;
            Init(logType, logLevel, start, end);
        }

        private void InitTableColumns()
        {
            this.tab_log.Columns = new ColumnCollection()
            {
                new Column("DeviceType", "设备类型", ColumnAlign.Center)
                {
                    Width="120",
                },
                new Column("Level", "等级",ColumnAlign.Center),
                new Column("Logger","日志位置"),
                new Column("Message","警告内容"),
                new Column("Date","时间")
            };
        }

        private void Init(string logType = null, string logLevel = null, DateTime? start = null, DateTime? end = null)
        {
            List<Models.Entity.Log.LogRecord> logList = DBHelper.HistoryDbContext.Select<EONFULLSIN.Models.Entity.Log.LogRecord>()
                 .WhereIf(!string.IsNullOrWhiteSpace(logType), x => x.DeviceType.Contains(logType))
                 .WhereIf(!string.IsNullOrWhiteSpace(logLevel), x => x.Level.Equals(logLevel))
                 .WhereIf(start != null, x => x.Date >= start)
                 .WhereIf(end != null, x => x.Date <= end)
                 .OrderByDescending(x => x.Date)
                 .Take(2000).ToList();
            tab_log.DataSource = logList;
        }
    }
}